package com.wise.service.cms.content.uploadresource;

import com.wise.entities.content.Resource;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.logger.Logger;
import com.wise.framework.restful.PageResultList;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.cms.content.uploadresources.ListUploadResources;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_100100000004;

/**
 * @author yubo
 */
@Service
@SummerService
public class ListUploadResourceService implements SummerServiceBean<ListUploadResources> {
    @Value("${azure.file.url}")
    private String url;

    private static final Logger log = Logger.getLogger(ListUploadResourceService.class);
    @Override
    public void sum(ListUploadResources summer) {
        PageResultList<Resource> list;
        SqlSelect select = SqlSelect.selec("*");
        select.fromClassName(Resource.class);
        if (StringUtils.isNotBlank(summer.getGroupId())) {
            select.where(Resource._groupId + "=?", summer.getGroupId());
        }
        if (StringUtils.isNotBlank(summer.getOriginResourceName())) {
            select.where(Resource._originResourceName + " like ?", "%" + summer.getOriginResourceName() + "%");
        }
        select.eqLang(Resource._lang);
        select.order_by(Resource._creationDate + " desc");
        list = DaoPageList.s(Resource.class, select, summer.getStart(), summer.getLimit());
        Precondition.checkNotNull(list, Err_100100000004);
//        if (StringUtils.isNotEmpty(url)) {
//            for (Resource resource : list.getList()) {
//                int index = resource.getPath().lastIndexOf("/");
//                String fileName = resource.getPath().substring(index + 1);
//                resource.setPath(url + fileName);
//                resource.setResourcePath(url + fileName);
//            }
//        }
        summer.setSummerResult(list);
    }
}
